﻿package kizzo.datastruct   
{
	
	public class ArrayIterator extends Iterator
	{
		private var array:Array;
		private var index:int;		
		
		/*
		 * Creates an array iterator for array.
		 */
		public function ArrayIterator(array:Array) 
		{
			this.array = array;
			this.index = 0;
		}
		
		/*
		 * Checks if there are more items to iterate over
		 */
		override public function hasNext():Boolean
		{
			return this.index <= (this.array.length - 1);
		}
		
		/*
		 * Returns the next array element.
		 */
		override public function getNext():*
		{						
			if (this.array[this.index])
				return this.array[this.index++];
			else
				throw new Error("Trying to get next item from iterator failed.");	
		}
		
		/*
		 * Resets the iteration process to its beginning, by seting the index pointer to 0.
		 */
		override public function reset():void
		{
			this.index = 0;
		}
		
	}
	
}